import 'package:flutter/material.dart';

/// 透明度动画
class DialogOpacityAnimation extends StatelessWidget {
  DialogOpacityAnimation({
    required this.controller,
    required this.child,
    Key? key,
  }) : super(key: key) {
    animation = CurvedAnimation(parent: controller, curve: Curves.ease);
  }

  final Widget child;
  final AnimationController controller;

  late final Animation<double> animation;

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: controller,
      builder: (BuildContext context, Widget? child) {
        return ClipRect(
          child: Opacity(
            opacity: animation.value,
            child: child,
          ),
        );
      },
      child: child,
    );
  }
}

/// 从底部弹出动画
class DialogBottomUpAnimation extends StatelessWidget {
  DialogBottomUpAnimation({
    required this.controller,
    required this.child,
    Key? key,
  }) : super(key: key) {
    // 位移
    tweenOffset = Tween<Offset>(begin: const Offset(0.0, 1.0), end: Offset.zero);

    animation = CurvedAnimation(parent: controller, curve: Curves.ease);
  }

  final AnimationController controller;
  final Widget child;

  late final Tween<Offset> tweenOffset;

  late final Animation<double> animation;

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: controller,
      builder: (BuildContext context, Widget? child) {
        return FractionalTranslation(
          translation: tweenOffset.evaluate(animation),
          child: ClipRect(
            child: Opacity(
              opacity: animation.value,
              child: child,
            ),
          ),
        );
      },
      child: child,
    );
  }
}

/// 中心显示动画
class CenterShowAnimation extends StatelessWidget {
  CenterShowAnimation({
    required this.controller,
    required this.child,
    Key? key,
  }) : super(key: key) {
    tweenOffset = Tween<Offset>(
      begin: const Offset(0.0, 0.2),
      end: Offset.zero,
    );
    tweenScale = Tween<double>(begin: 0.3, end: 1.0);
    animation = CurvedAnimation(parent: controller, curve: Curves.ease);
  }

  final AnimationController controller;
  final Widget child;

  late final Tween<Offset> tweenOffset;
  late final Tween<double> tweenScale;

  late final Animation<double> animation;

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: controller,
      builder: (BuildContext context, Widget? child) {
        return FractionalTranslation(
          translation: tweenOffset.evaluate(animation),
          child: ClipRect(
            child: Transform.scale(
              scale: tweenScale.evaluate(animation),
              child: Opacity(
                opacity: animation.value,
                child: child,
              ),
            ),
          ),
        );
      },
      child: child,
    );
  }
}
